package com.erps.CommonUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class TextFileUtils {
	private TextFileUtils() {

	}

	public static void CheckDir(String cDir) {
		File vDir = new File(cDir);
		if (!vDir.exists())
			vDir.mkdir();
	}

	public static void WriteTextFile(String cStr) {
		if (cStr == null)
			return;
		if (cStr.length() == 0)
			return;
		TextFileUtils v = new TextFileUtils();
		String cDir = v.getClass().getResource("/").getFile().toString();
		String cFileName = cDir + File.separatorChar + "RuleCheckLog.txt";
		File f = new File(cFileName);
		FileOutputStream out;
		try {
			out = new FileOutputStream(f);
			OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8");
			BufferedWriter bwr = new BufferedWriter(osw);

			bwr.write(cStr);
			bwr.flush();
			bwr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static boolean WriteTextFile(String cFileName, String cFileValue) {
		if (cFileValue == null)
			return false;
		if (cFileValue.length() == 0)
			return false;
		File f = new File(cFileName);
		FileOutputStream out;
		try {
			out = new FileOutputStream(f);
			OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8");
			BufferedWriter bwr = new BufferedWriter(osw);

			bwr.write(cFileValue);
			bwr.flush();
			bwr.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	public static String readTextFile(String cFileName) {
		File file = new File(cFileName);
		FileInputStream in;
		try {
			in = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
		InputStreamReader isr = null;
		try {
			isr = new InputStreamReader(in, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
		BufferedReader br = new BufferedReader(isr);
		String content = "";
		String s1 = null;
		try {
			while ((s1 = br.readLine()) != null) {
				content += s1;
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return content;
	}
}
